%
% Author: Vasyl Mykhalchuk
%
function [ ] = sf( dataFile, name, epsilon, numFrames )
% Modify matlab path
addpath(genpath('../util')); addpath(genpath('../io')); addpath(genpath('../strain'));
addpath(genpath('../export')); addpath(genpath('../obj-tools')); addpath(genpath('../preprocess'));
addpath(genpath('..')); addpath( genpath('../cloth') ); addpath( genpath ('../scalar-field') );
addpath( genpath('../neighborhood') );

resDir = ['../../res/' name '/'];
% Load data
data = load( [resDir dataFile] );

[numberOfFrames, nV] = size(data.frames);
nV = nV / 3;
vertices = toMatrix(data.frames(1,:));
obj_ref.vertices = vertices;
obj_ref.faces = data.topology;
nF = size(data.topology, 1);

obj_ref.nF = nF;
obj_ref.nV = nV;

colors = zeros(nV, 3);
for i=1:nV
    for j = 1:3
        colors(i,j)=255;
    end
end

exportPLY( 'fem4.ply', obj_ref.vertices, colors, obj_ref.faces,0);
return;

if (numFrames ~= 0)
numberOfFrames = numFrames;
end % if

% default initialization to zeroes
sfMinGlobal = zeros(obj_ref.nV, 1);
sfMaxGlobal = zeros(obj_ref.nV, 1);
sfMeanGlobal = zeros(obj_ref.nV, 1);
sfTMeanGlobal = zeros(obj_ref.nF, 1);

defSF = zeros(obj_ref.nV, numberOfFrames); 
defSFT = zeros(obj_ref.nF, numberOfFrames);

% compute adjacency lists
% List of faces adjacent to each vertex

%{
LOG('Preprocessing');
adjList = createAdjacencyList(obj_ref.faces, obj_ref.nV, obj_ref.nF);
% List contains list of neighboring vertices for each vertex
adjVertList = createVertexAdjacencyList(obj_ref.faces, obj_ref.nV, obj_ref.nF);
save('elephant.mat', 'adjList', 'adjVertList');
%}

%TestAnimatedMesh( [resDir name] );

% Load Adj list
hmm = load( [resDir name 'Adj.mat'] );
adjList = hmm.adjList;
adjVertList = hmm.adjVertList;
% Load dist matrix

distMatrix = load( [resDir name 'DistMatrix.mat'] );
distMatrix = distMatrix.distMatrix;


% DEFORMATION FIELD
%deformationField = zeros(obj_ref.nV, numberOfFrames);

% STEP THROUGH FRAMES
stepCount = 0;
for targetId = 2:numberOfFrames
    vertices = toMatrix(data.frames(targetId,:));
    obj_target.vertices = vertices;
    obj_target.faces = data.topology;
    
    [sf, sfT] = nextSF(obj_target, obj_ref, adjList, adjVertList);
    
    sfMeanGlobal = sfMeanGlobal + sf;
    sfTMeanGlobal = sfTMeanGlobal + sfT;
    stepCount = stepCount + 1;
    [sfMinGlobal, sfMaxGlobal] = calculateMinMax(sf, sfMinGlobal, sfMaxGlobal);
    %deformationField(:, stepCount) = sf;
    defSF(:, stepCount) = sf;
    defSFT(:, stepCount) = sfT;
end%for

% CALCULATE ENTROPIES
n = nV;
%{
hSF = zeros(obj_ref.nV, 1);
for i = 1:obj_ref.nV
    hSF(i) = entropy( deformationField(i, :), (4 / 5) * numberOfFrames);
end%for
%}

% CALCULATE MEAN DURING THE ANIMATION
sfMeanGlobal = sfMeanGlobal / stepCount;
sfTMeanGlobal = sfTMeanGlobal / stepCount;

% SAVE RESULTS
outDir = 'target/';
LOG('Exporting... sfMax');

colors = colorForField(sfMaxGlobal, -3);
%colors = colorForField( defSF(:,1) );
%colors = colorForField(hSF, n);

featPoints = extractFPs(sfMaxGlobal, adjVertList, distMatrix, epsilon);


nFP = size(featPoints, 1);
pinkColor = [255 0 255];
for i = 1:nFP
    featPoints(i)
    colors( featPoints(i), :) = pinkColor;
end % for

path = getExportFileName(outDir, name, exportFileType.kSFMean);
vertices = toMatrix(data.frames(22,:));
ref = load('horseRef.mat');
vertices = ref.horseRef;
exportPLY( path, vertices, colors, obj_ref.faces, 0);

return;

%vertexColorList = createColorInfo(allVertices, sfMax, h);
path = getExportFileName(outDir, name, exportFileType.kSFMax);
exportPLY( path, obj_ref.vertices, colors, obj_ref.faces);



end % function 